package com.study.bookstore.service.impl;

import com.alibaba.fastjson.util.TypeUtils;
import com.study.bookstore.service.BookTypeService;
import com.study.bookstore.domain.BookType;
import com.study.bookstore.repository.BookTypeRepository;
import com.study.bookstore.service.dto.DtoBookType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Service Implementation for managing BookType.
 */
@Service
@Transactional
public class BookTypeServiceImpl implements BookTypeService {

    private final Logger log = LoggerFactory.getLogger(BookTypeServiceImpl.class);

    private final BookTypeRepository bookTypeRepository;

    public BookTypeServiceImpl(BookTypeRepository bookTypeRepository) {
        this.bookTypeRepository = bookTypeRepository;
    }

    @Autowired
    private JdbcTemplate jdbcTemplate;
    /**
     * Save a bookType.
     *
     * @param bookType the entity to save
     * @return the persisted entity
     */
    @Override
    public BookType save(BookType bookType) {
        log.debug("Request to save BookType : {}", bookType);
        return bookTypeRepository.save(bookType);
    }

    /**
     * Get all the bookTypes.
     *
     * @param pageable the pagination information
     * @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<BookType> findAll(Pageable pageable) {
        log.debug("Request to get all BookTypes");
        return bookTypeRepository.findAll(pageable);
    }

    /**
     * Get one bookType by id.
     *
     * @param id the id of the entity
     * @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public BookType findOne(Long id) {
        log.debug("Request to get BookType : {}", id);
        return bookTypeRepository.findOne(id);
    }

    /**
     * Delete the bookType by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete BookType : {}", id);
        bookTypeRepository.delete(id);
    }

    @Override
    public List<DtoBookType> getDtoBookType() throws Exception {
        return getAllDtoBookType("全部分类");
    }
    @Override
    public List<DtoBookType> getAllDtoBookType(String p_type) throws Exception {
        String sql="SELECT a.*,(" +
            "SELECT COUNT(*) FROM `book_type` WHERE book_type.`p_type`=a.`jhi_type`) AS leaf\n" +
            " FROM `book_type` a WHERE a.p_type='"+p_type+"'";
        List<Map<String,Object>> queryList=jdbcTemplate.queryForList(sql);
        List<DtoBookType> list = new ArrayList<>();
        for (Map<String,Object> item: queryList) {
            DtoBookType dtoBookType = new DtoBookType();
            dtoBookType.setType(TypeUtils.castToString(item.get("jhi_type")));
            dtoBookType.setContent(TypeUtils.castToString(item.get("content")));
            if(TypeUtils.castToInt(item.get("leaf"))>0){
                dtoBookType.setLeaf(true);
            } else {
                dtoBookType.setLeaf(false);
            }
            if(dtoBookType.getLeaf()){
                dtoBookType.setChildren(getAllDtoBookType(dtoBookType.getType()));
            }
            list.add(dtoBookType);
        }
        return list;
    }
}
